package heven.holt.eyepetizer.widget

import android.content.Context
import android.graphics.Typeface
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView
import heven.holt.eyepetizer.R

class TypefaceTextView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = android.R.attr.editTextStyle
) : AppCompatTextView(context, attrs, defStyleAttr) {
    init {
        attrs?.let {
            val typedArray = context.obtainStyledAttributes(it, R.styleable.TypefaceTextView, 0, 0)
            val typefaceType = typedArray.getInt(R.styleable.TypefaceTextView_typeface, 0)
            typeface = getTypeFace(typefaceType)
            typedArray.recycle()
        }
    }

    private fun getTypeFace(typefaceType: Int): Typeface = when (typefaceType) {
        FZLL_TYPEFACE -> {
            Typeface.createFromAsset(context.assets, "fonts/FZLanTingHeiS-L-GB-Regular.TTF")
        }
        FZDB1_TYPEFACE -> {
            Typeface.createFromAsset(context.assets, "fonts/FZLanTingHeiS-DB1-GB-Regular.TTF")
        }
        FUTURA_TYPEFACE -> {
            Typeface.createFromAsset(context.assets, "fonts/Futura-CondensedMedium.ttf")
        }
        DIN_TYPEFACE -> {
            Typeface.createFromAsset(context.assets, "fonts/DIN-Condensed-Bold.ttf")
        }
        LOBSTER_TYPEFACE -> {
            Typeface.createFromAsset(context.assets, "fonts/Lobster-1.4.otf")
        }
        else -> Typeface.DEFAULT
    }

    companion object {
        private const val FZLL_TYPEFACE = 1

        private const val FZDB1_TYPEFACE = 2

        private const val FUTURA_TYPEFACE = 3

        private const val DIN_TYPEFACE = 4

        private const val LOBSTER_TYPEFACE = 5
    }
}